package com.wq.jdk8;

import java.util.function.Function;

/**
 * @Author: wangqiang20995
 * @Date:2019/9/5
 * @Description:jdk8的Function接口使用demo
 * @Resource:
 */
public class FunctionalDemo {

    public static void main(String[] args) {
        FunctionalDemo demo = new FunctionalDemo();
        demo.simpleDemo();

        demo.compseAndThen();
    }

    /**
     * apply:根据输入参数，自定义函数的计算规则，返回泛型中指定的输出类型
     * compose:compose接受一个Function作为一个入参，同时返回一个Function作为出参，但是先调用入参的apply方法获取result，然后将result作为param传给出参的Function
     * andThen:andThen接受一个Function作为入参，返回一个Function作为出参，区别就是先调用作为出参的Function#apply，得到一个结果result，然后将result作为param传给入参的Function
     */
    public void simpleDemo() {
        Function<Integer, Integer> function = a -> a + 2;

        System.out.println(function.apply(1));
    }

    public void compseAndThen() {
        Function<String, String> before = name -> "before 输入参数[" + name + "]";
        Function<String, String> after = name -> "after 输入参数[" + name + "]";

        System.out.println("compose:" + after.compose(before).apply("George"));
        System.out.println("before:" + before.andThen(after).apply("Jane"));
    }
}
